﻿package com.bizmlib.utils
{
	
	public class StringUtils extends Object
	{
		
		public static function addCharacters(sourceString:String, character:String = " ", limit:Number = 2, position:Number = 0):String
		{
			if(sourceString)
			{
				while(sourceString.length<limit)
				{
					if(position==0)
					{
						sourceString = character + sourceString;
					}
					else
					{
						sourceString += character;
					}
				}
				
				return sourceString;
			}
			else
			{
				return "";
			}
		}
		
		public static function formatDateTime(date:Date, formatString:String):String
		{
			if(!date)
				return "";
			
			var resultString:String = formatString;
			var re:RegExp;
			var year:String = String(date.fullYear);
			var month:String = String(date.month + 1);
			var dateValue:String = String(date.date);
			var hours:Number = date.hours;
			var hoursFormattingPhase:int = 0;
			var minutes:String = String(date.minutes);
			var seconds:String = String(date.seconds);
			var doYearReplacement:Function = function(...rest):String
			{
				var localYear:String = year;
				if(rest[2].length < localYear.length)
					localYear = localYear.substr(-rest[2].length, rest[2].length);
				return rest[1] + localYear + rest[3];
			};
			var doMonthReplacement:Function = function(...rest):String
			{
				return rest[1] + addCharacters(month, "0", rest[2].length, 0) + rest[3];
			};
			var doDateReplacement:Function = function(...rest):String
			{
				return rest[1] + addCharacters(dateValue, "0", rest[2].length, 0) + rest[3];
			};
			var doHoursReplacement:Function = function(...rest):String
			{
				var localHours:String;
				switch(hoursFormattingPhase)
				{
					case 2:
						localHours = String(hours % 12);
						break;
					case 1:
					default:
						localHours = String(hours)
						break;
				}
				return rest[1] + localHours + rest[3];
			};
			var doMinutesReplacement:Function = function(...rest):String
			{
				return rest[1] + addCharacters(minutes, "0", rest[2].length, 0) + rest[3];
			};
			var doSecondsReplacement:Function = function(...rest):String
			{
				return rest[1] + addCharacters(seconds, "0", rest[2].length, 0) + rest[3];
			};
			
			re = /([^y]*)(y+)([^y]*)/gi;
			resultString = resultString.replace(re, doYearReplacement);
			
			re = /([^m]*)(m+)([^m]*)/gi;
			resultString = resultString.replace(re, doMonthReplacement);
			
			re = /([^d]*)(d+)([^d]*)/gi;
			resultString = resultString.replace(re, doDateReplacement);
			
			hoursFormattingPhase = 1;
			re = /([^h]*)(h+)([^h]*)/gi;
			resultString = resultString.replace(re, doHoursReplacement);
			
			hoursFormattingPhase = 2;
			re = /([^l]*)(l+)([^l]*)/gi;
			resultString = resultString.replace(re, doHoursReplacement);
			
			re = /([^n]*)(n+)([^n]*)/gi;
			resultString = resultString.replace(re, doMinutesReplacement);
			
			re = /([^s]*)(s+)([^s]*)/gi;
			resultString = resultString.replace(re, doSecondsReplacement);
			
			return resultString;
		}
	}
}